package com.project.blog.controller;


import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.blog.common.Result;
import com.project.blog.dto.UserQuery;
import com.project.blog.entity.User;
import com.project.blog.utils.JwtUtils;
import com.project.blog.utils.Utils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author 代码奔腾
 * @since 2022-12-29
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @GetMapping("/")
    public List<User> list() {
        return userService.list();
    }

    @PostMapping("/page")
    public Result<?> findPage(@RequestBody UserQuery userQuery) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(User::getId);
        if (!StringUtils.isBlank(userQuery.getUsername())) {
            wrapper.like(User::getUsername, userQuery.getUsername());
        }
        Page<User> page = userService.page(new Page<>(
                        userQuery.getPageNum(),
                        userQuery.getPageSize()),
                wrapper
        );
        return Result.success(page);
    }

//    注册用户接口
    @PostMapping("/save")
    public Object save(@Validated @RequestBody User user) {
        String password = user.getPassword();
        String salt = Utils.salt();
        String resultPassword = SecureUtil.md5(password + salt);
        user.setPassword(resultPassword);
        userService.saveOrUpdate(user);
        return Result.success();
    }

    @PostMapping("/delBatch")
    public Object deleteBatch(@RequestBody List<Integer> ids) {
//        throw new CustomException(500,"删除错误");
        userService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping("/login")
    public Result<?> login(@RequestParam("username") String username, @RequestParam("password") String password) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username)

//                .eq(User::getPassword, SecureUtil.md5(password))
                .last("limit 1");


        User userInfo = userService.getOne(wrapper);
        HashMap<Object, Object> returnInfo = new HashMap<>();
        if (userInfo != null) {
            if (userInfo.getPassword() != null) {
                if (userInfo.getPassword().equals(SecureUtil.md5(password + userInfo.getSalt()))) {
                    String token = JwtUtils.generateToken(userInfo);
                    HashMap<Object, Object> map = new HashMap<>();
                    map.put("id", userInfo.getId());
                    map.put("username", userInfo.getUsername());
                    map.put("email", userInfo.getEmail());
                    map.put("roleType", userInfo.getRoleType());
                    map.put("token", token);
                    returnInfo.put("userInfo", map);
                    returnInfo.put("menuList", "");
                    return Result.success(map);
                }
            }
        }
        return Result.error("请检查用户密码是否正确");
    }


}
